//
//  ViewController.swift
//  New
//
//  Created by 李仁强 on 2016/9/30.
//  Copyright © 2016年 李仁强. All rights reserved.
//

import UIKit
let kScreenWidth = UIScreen.mainScreen().bounds.size.width
let kScreenHeight = UIScreen.mainScreen().bounds.size.height
let kScreenBounds = UIScreen.mainScreen().bounds
let collectionCellId = "collectionCellId"
let leftMargin : CGFloat = 226.0 / 750.0 * kScreenWidth
let rightMargin : CGFloat = leftMargin
let btmMargin : CGFloat = 182.0 / 1334.0 * kScreenHeight

class ViewController: UIViewController,  UICollectionViewDataSource, UICollectionViewDelegate{

    var imagesArr : [String] = ["001", "002", "003", "004"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        

        setupUI()
        
    }
    
    func setupUI() {
        
        //layout
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        
        //collection view
        let collectionView = UICollectionView(frame: kScreenBounds, collectionViewLayout: layout)
        view.addSubview(collectionView)
        collectionView.backgroundColor = UIColor.whiteColor()
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionCellId)
        collectionView.pagingEnabled = true
    }
    
    lazy var startBtn : UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "button"), forState: .Normal)
        btn.addTarget(self, action: #selector(startBtnClick), forControlEvents: .TouchUpInside)
        return btn
        
    }()

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

// collection view delegat function
extension ViewController {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imagesArr.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collectionCellId, forIndexPath: indexPath)
        
        let imageName = imagesArr[indexPath.item]
        let imgView = UIImageView(frame: kScreenBounds)
        imgView.image = UIImage(named: imageName)
        cell.contentView.addSubview(imgView)
        
        if indexPath.item == (imagesArr.count - 1) {
            cell.contentView.addSubview(startBtn)
            let btnW = 300.0 / 750.0 * kScreenWidth
            let btnH = 60.0 / 1334.0 * kScreenHeight
            let btnX = leftMargin
            let btnY = kScreenHeight - btnH - btmMargin
            startBtn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
        }
        
        return cell
    }
    
    @objc func startBtnClick() {
        //start button click event
        print("btnclick")
    }
    
    
}


























